package net.hvacapp.cmvrfforandroid.ui.mine;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;

import net.hvacapp.cmvrfforandroid.AppManager;
import net.hvacapp.cmvrfforandroid.R;
import net.hvacapp.cmvrfforandroid.ui.BaseActivity;
import net.hvacapp.cmvrfforandroid.ui.selections.SelectionCityActivity;
import net.hvacapp.cmvrfforandroid.ui.selections.SelectionNationActivity;
import net.hvacapp.cmvrfforandroid.ui.selections.SelectionProvinceActivity;
import net.hvacapp.cmvrfforandroid.ui.selections.model.AreaData;
import net.hvacapp.cmvrfforandroid.utils.AnnotationUtil;
import net.hvacapp.cmvrfforandroid.utils.DateTimePickDialogUtil;
import net.hvacapp.cmvrfforandroid.widget.UserSelectionView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

@EActivity(R.layout.activity_statistics_filter)
public class StatisticsFilterActivity extends BaseActivity {

    private final int Select_Nation = 1;
    private final int Select_Province = 2;
    private final int Select_City = 3;

    @ViewById(R.id.sel_start)
    UserSelectionView mStartSel;
    @ViewById(R.id.sel_end)
    UserSelectionView mEnd;


    @ViewById(R.id.sel_nation)
    UserSelectionView mNationSel;
    @ViewById(R.id.sel_province)
    UserSelectionView mProvinceSel;
    @ViewById(R.id.sel_city)
    UserSelectionView mCitySel;

    @ViewById(R.id.btn_view)
    TextView mViewTv;

    private ArrayList<AreaData> mSelectedNation;
    private ArrayList<AreaData> mSelectedProvince;
    private ArrayList<AreaData> mSelectedCity;

    private String mGuidId;
    private String mTitle;
    private String mStartDate;
    private String mEndDate;


    /**
     * 页面加载完后执行
     */
    @AfterViews
    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();

        initEvent();
    }

    /**
     * 初始化
     */
    @Override
    public void initBoot() {

    }

    /**
     * 初始化数据
     */
    @Override
    public void initData(Intent intent) {
        mGuidId = intent.getStringExtra("guidId");
        mTitle = intent.getStringExtra("title");
        setupToolbar(0, mTitle, null);

        mStartSel.setTitleString("开始时间");
        mEnd.setTitleString("结束时间");
        mNationSel.setTitleString("选择国家");
        mProvinceSel.setTitleString("选择省份");
        mCitySel.setTitleString("选择城市");

        AreaData defaultNation = AppManager.getAppManager().getmDefaultNation();
        if (defaultNation != null) {
            mSelectedNation = new ArrayList<>();
            mSelectedNation.add(defaultNation);
            mNationSel.setInfoString(defaultNation.getName());
        }

        //结束时间
        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        calendar.setTime(date);
        mEndDate = String.format("%d%02d%02d", calendar.get(Calendar.YEAR), (calendar.get(Calendar.MONTH) + 1), calendar.get(Calendar.DAY_OF_MONTH));
        mEnd.setInfoString(mEndDate);

        //开始时间
        calendar.add(Calendar.MONTH, -1);
        mStartDate = String.format("%d%02d%02d", calendar.get(Calendar.YEAR), (calendar.get(Calendar.MONTH) + 1), calendar.get(Calendar.DAY_OF_MONTH));
        mStartSel.setInfoString(mStartDate);

    }



    private void initEvent() {
        mNationSel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(StatisticsFilterActivity.this, AnnotationUtil.get(SelectionNationActivity.class));
                Bundle bundle = new Bundle();
                bundle.putParcelableArrayList("nation", mSelectedNation);
                bundle.putBoolean("multiple", false);
                intent.putExtras(bundle);
                startActivityForResult(intent, Select_Nation);
            }
        });

        mProvinceSel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSelectedNation == null || mSelectedNation.size() == 0) {
                    showMessage("请先选择国家！");
                }
                else {
                    Intent intent = new Intent(StatisticsFilterActivity.this, AnnotationUtil.get(SelectionProvinceActivity.class));
                    Bundle bundle = new Bundle();
                    bundle.putParcelableArrayList("nation", mSelectedNation);
                    bundle.putParcelableArrayList("province", mSelectedProvince);
                    bundle.putBoolean("multiple", false);
                    intent.putExtras(bundle);
                    startActivityForResult(intent, Select_Province);
                }
            }
        });
        mCitySel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSelectedProvince == null || mSelectedProvince.size() == 0) {
                    showMessage("请先选择省份！");
                }
                else {
                    Intent intent = new Intent(StatisticsFilterActivity.this, AnnotationUtil.get(SelectionCityActivity.class));
                    Bundle bundle = new Bundle();
                    bundle.putParcelableArrayList("province", mSelectedProvince);
                    bundle.putParcelableArrayList("city", mSelectedCity);
                    bundle.putBoolean("multiple", false);
                    intent.putExtras(bundle);
                    startActivityForResult(intent, Select_City);
                }
            }
        });
        mStartSel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //开始时间
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.MONTH, -1);

                DatePickerDialog dlg = new DatePickerDialog(StatisticsFilterActivity.this,
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                //Calendar月份是从0开始,所以month要加1
                                mStartDate = String.format("%d%02d%02d", year, (monthOfYear+1), dayOfMonth);
                                mStartSel.setInfoString(mStartDate);
                            }
                        },
                        calendar.get(Calendar.YEAR),
                        calendar.get(Calendar.MONTH),
                        calendar.get(Calendar.DAY_OF_MONTH));
                dlg.show();

            }
        });
        mEnd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //结束时间
                Calendar calendar = Calendar.getInstance();

                DatePickerDialog dlg = new DatePickerDialog(StatisticsFilterActivity.this,
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                //Calendar月份是从0开始,所以month要加1
                                mEndDate = String.format("%d%02d%02d", year, (monthOfYear+1), dayOfMonth);
                                mEnd.setInfoString(mEndDate);
                            }
                        },
                        calendar.get(Calendar.YEAR),
                        calendar.get(Calendar.MONTH),
                        calendar.get(Calendar.DAY_OF_MONTH));
                dlg.show();
            }
        });

        mViewTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //查看统计结果
                String rid;
                String area;
                if (mSelectedCity != null && mSelectedCity.size() > 0) {
                    rid = mSelectedCity.get(0).getGuidId();
                    area = mSelectedCity.get(0).getName();
                }
                else if (mSelectedNation != null && mSelectedNation.size() > 0) {
                    rid = mSelectedNation.get(0).getGuidId();
                    area = mSelectedNation.get(0).getName();
                }
                else if (mSelectedNation != null && mSelectedNation.size() > 0) {
                    rid = mSelectedNation.get(0).getGuidId();
                    area = mSelectedNation.get(0).getName();
                }
                else {
                    showMessage("请选择要过滤的地区！");
                    return;
                }
                Intent intent = new Intent(StatisticsFilterActivity.this, AnnotationUtil.get(StatisticsBrowserActivity.class));
                Bundle bundle = new Bundle();
                bundle.putString("guidId", mGuidId);
                bundle.putString("title", mTitle);
                bundle.putString("tf", mStartDate);
                bundle.putString("tt", mEndDate);
                bundle.putString("rid", rid);
                bundle.putString("area", area);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    /**
     * 页面名称
     */
    @Override
    public String getName() {
        return "";
    }

    /**
     * 回调
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != this.RESULT_OK) {
            return;
        }
        if (requestCode == Select_Nation) {
            if (data != null) {
                mSelectedNation = data.getParcelableArrayListExtra("nation");
                String selStr = "";
                if (mSelectedNation != null && mSelectedNation.size() > 0) {
                    for (AreaData item : mSelectedNation) {
                        selStr += item.getName();
                        selStr += " ";
                    }
                }
                mNationSel.setInfoString(selStr);
            }
        } else if (requestCode == Select_Province) {
            if (data != null) {
                mSelectedProvince = data.getParcelableArrayListExtra("province");
                String selStr = "";
                if (mSelectedProvince != null && mSelectedProvince.size() > 0) {
                    if (mSelectedCity != null) {
                        mSelectedCity.clear();
                        mCitySel.setInfoString("");
                    }
                    for (AreaData item : mSelectedProvince) {
                        selStr += item.getName();
                        selStr += " ";
                    }
                }
                mProvinceSel.setInfoString(selStr);
            }
        } else if (requestCode == Select_City) {
            if (data != null) {
                mSelectedCity = data.getParcelableArrayListExtra("city");
                String selStr = "";
                if (mSelectedCity != null && mSelectedCity.size() > 0) {
                    for (AreaData item : mSelectedCity) {
                        selStr += item.getName();
                        selStr += " ";
                    }
                }
                mCitySel.setInfoString(selStr);
            }
        }
    }
}
